// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

using DataModel;
using CommonGui;
using WebLib;


namespace Ged2Xml
{
    public partial class Ged2XmlForm : Form
    {
        private GedComImportSettings gedComImportSettings = new GedComImportSettings();
        private WebSiteSettings webSiteSettings = new WebSiteSettings();


        public Ged2XmlForm()
        {
            InitializeComponent();
        }

        private void CloseButton_Click(object sender, EventArgs ea)
        {
            Application.Exit();
        }



        private void DoFamilyXml()
        {
            // Try to open the input file...
            FileInfo inputInfo = new FileInfo(InputFileTextBox.Text);
            if (!inputInfo.Exists)
            {
                MessageBox.Show(this, "Could not find input file!", "Boom!");
                return;
            }

            // Make sure they really wanna overwrite the output file, if it exists...
            FileInfo outputInfo = new FileInfo(this.FamilyOutputTextBox.Text);

            if (outputInfo.Exists)
            {
                DialogResult result = MessageBox.Show(this, "Output file already exists, are you sure you want to overwrite it?", "Overwrite?", MessageBoxButtons.YesNo);

                if (result != DialogResult.Yes)
                {
                    return;
                }
            }

            // Give 'em a pretty cursor...the caller will reset back to the default.
            this.Cursor = Cursors.WaitCursor;

            // Read in the GEDCOM info and put it into our internalized format...
            GedComReadResults gedResults = new GedComReadResults(inputInfo);

            GedComFile ged = GedComFile.Read(inputInfo, gedResults);

            // Convert the gedcom file to the internal representation...
            GedComImportResults importResults = new GedComImportResults(inputInfo);

            FamilyInfo family = FamilyInfo.LoadFromGedCom(ged, gedComImportSettings, importResults);

            // Show the import results...
            GedComImportResultsDialog dialog = new GedComImportResultsDialog(importResults, gedResults);
            dialog.ShowDialog(this);
            this.Cursor = Cursors.WaitCursor;

            // ...write it out to one big Xml file...
            SaveResults saveResults = new SaveResults(outputInfo);

            family.Save(outputInfo, saveResults);

            // Show the save results...
            SaveResultsDialog saveResultsDialog = new SaveResultsDialog(saveResults);
            saveResultsDialog.ShowDialog(this);
        }


        private void DoDebugXml()
        {
            // Try to open the input file...
            FileInfo inputInfo = new FileInfo(InputFileTextBox.Text);
            if (!inputInfo.Exists)
            {
                MessageBox.Show(this, "Could not find input file!", "Boom!");
                return;
            }

            // Make sure they really wanna overwrite the output file, if it exists...
            FileInfo outputInfo = new FileInfo(this.DebugOutputTextBox.Text);

            if (outputInfo.Exists)
            {
                DialogResult result = MessageBox.Show(this, "Output file already exists, are you sure you want to overwrite it?", "Overwrite?", MessageBoxButtons.YesNo);

                if (result != DialogResult.Yes)
                {
                    return;
                }
            }

            // Read in the GEDCOM info and put it into our internalized format...
            GedComReadResults gedResults = new GedComReadResults(inputInfo);

            GedComFile ged = GedComFile.Read(inputInfo, gedResults);

            // Write the info out in debug format...
            ged.WriteDebugXml(outputInfo);

            // Let them know we probably did what they wanted...
            // TODO - Doug - 8/12/2006 - pop up a dialog to show the gedResults

            MessageBox.Show(this, "GedCom file read and saved as debug Xml format.", "Done");
        }



        private void DoWebSite()
        {
            // Try to open the input file...
            FileInfo inputInfo = new FileInfo(InputFileTextBox.Text);
            if (!inputInfo.Exists)
            {
                MessageBox.Show(this, "Could not find input file!", "Boom!");
                return;
            }

            // Make sure they really wanna overwrite the output file, if it exists...
            DirectoryInfo outputInfo = new DirectoryInfo(WebSiteOutputTextBox.Text);
            if (outputInfo.Exists)
            {
                if (outputInfo.GetFiles("*.htm").Length > 0)
                {
                    if (MessageBox.Show(this, "Output directory already contains a web site, do you wish to overwrite it?", "Overwrite web site?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        // TODO - Doug - 8/1/2006 - clean out ALL the files in the directory, not just the .htm files...
                        foreach (FileInfo file in outputInfo.GetFiles("*.htm"))
                        {
                            file.Delete();
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else
            {
                if (MessageBox.Show(this, "Output directory does not exist.  Do you wish to create it?", "Create Directory?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    outputInfo.Create();
                }
                else
                {
                    return;
                }
            }

            // Give 'em a pretty cursor...the caller will reset back to the default.
            this.Cursor = Cursors.WaitCursor;

            // Read in the GEDCOM info and put it into our internalized format...
            GedComReadResults gedResults = new GedComReadResults(inputInfo);

            GedComFile ged = GedComFile.Read(inputInfo, gedResults);

            // Convert the gedcom file to the internal representation...
            GedComImportResults importResults = new GedComImportResults(inputInfo);

            FamilyInfo family = FamilyInfo.LoadFromGedCom(ged, this.gedComImportSettings, importResults);

            // Show the import results...
            GedComImportResultsDialog importResultsDialog = new GedComImportResultsDialog(importResults, gedResults);
            importResultsDialog.ShowDialog(this);

            this.Cursor = Cursors.WaitCursor;
            this.Refresh();

            // Set up the web site results...
            WebSiteResults webSiteResults = new WebSiteResults(outputInfo);

            // Generate the web site...
            WebSite web = new WebSite();

            web.Generate(outputInfo, family, this.webSiteSettings, webSiteResults);

            // Show them the results dialog...
            WebSiteResultsDialog webSiteResultsDialog = new WebSiteResultsDialog(webSiteResults);
            webSiteResultsDialog.ShowDialog(this);
        }


        private void GoButton_Click(object sender, EventArgs ea)
        {
            try
            {
                // Do the proper thing, based on which radio button is checked...
                if (this.FamilyXmlRadioButton.Checked)
                {
                    DoFamilyXml();
                }
                else if (this.DebugXmlRadioButton.Checked)
                {
                    DoDebugXml();
                }
                else if (this.WebSiteRadioButton.Checked)
                {
                    DoWebSite();
                }
                else if (this.GedComRadioButton.Checked)
                {
                    // TODO - Doug - 8/1/2006 - implement gedcom export
                    MessageBox.Show(this, "GedCom export is not yet implemented.  Please try again later.", "Not yet implemented.");
                }
            }
            // TODO - Doug - 8/3/2006 - Put this back in place...for now, it's ok for these to bubble out...
            //catch (Exception e)
            //{
            //    MessageBox.Show(this, e.ToString(), "Boom!");
            //}
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void InputBrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "GEDCOM Files (*.ged)|*.ged|All files (*.*)|*.*";
            dialog.DefaultExt = "ged";

            if (!string.IsNullOrEmpty(InputFileTextBox.Text))
            {
                FileInfo info = new FileInfo(InputFileTextBox.Text);
                if ((info.Directory != null) && (info.Directory.Exists))
                {
                    dialog.InitialDirectory = info.Directory.FullName;
                }
            }

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                InputFileTextBox.Text = dialog.FileName;
            }
        }


        private void FamilyOutputBrowseButton_Click(object sender, EventArgs e)
        {
            // TODO - Doug - 8/1/2006 - implement the family output browse button
            MessageBox.Show(this, "Family output browse button is not yet implemented.  Please try again later.", "Not yet implemented.");
        }


        private void DebugOutputBrowseButton_Click(object sender, EventArgs e)
        {
            // TODO - Doug - 8/1/2006 - implement the debug output browse button
            MessageBox.Show(this, "Debug output browse button is not yet implemented.  Please try again later.", "Not yet implemented.");
        }


        private void WebSiteOutputBrowseButton_Click(object sender, EventArgs e)
        {
            // TODO - Doug - 8/1/2006 - implement the web site output browse button
            MessageBox.Show(this, "Web site output browse button is not yet implemented.  Please try again later.", "Not yet implemented.");
        }


        private void FamilyOptionsButton_Click(object sender, EventArgs e)
        {
            GedComImportSettingsDialog dialog = new GedComImportSettingsDialog();

            dialog.Settings = this.gedComImportSettings.Clone();

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.gedComImportSettings = dialog.Settings;
            }
        }

        private void WebSiteSettingsButton_Click(object sender, EventArgs e)
        {
            WebSiteSettingsDialog dialog = new WebSiteSettingsDialog();

            dialog.Settings = this.webSiteSettings.Clone();

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.webSiteSettings = dialog.Settings;
            }
        }

        private void FamilyXmlRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            bool isChecked = this.FamilyXmlRadioButton.Checked;
            this.FamilyOutputTextBox.Enabled = isChecked;
            this.FamilyOutputBrowseButton.Enabled = isChecked;
            this.FamilyOptionsButton.Enabled = isChecked;
        }

        private void DebugXmlRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            bool isChecked = this.DebugXmlRadioButton.Checked;
            this.DebugOutputTextBox.Enabled = isChecked;
            this.DebugOutputBrowseButton.Enabled = isChecked;
        }

        private void WebSiteRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            bool isChecked = this.WebSiteRadioButton.Checked;
            this.WebSiteOutputTextBox.Enabled = isChecked;
            this.WebSiteOutputBrowseButton.Enabled = isChecked;
            this.WebSiteSettingsButton.Enabled = isChecked;
        }

        private void GedComRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            bool isChecked = this.GedComRadioButton.Checked;
            this.GedComOutputBrowseButton.Enabled = isChecked;
            this.GedComOutputTextBox.Enabled = isChecked;
        }

        private void GedComOutputBrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "GEDCOM Files (*.ged)|*.ged|All files (*.*)|*.*";
            dialog.CheckFileExists = false;
            dialog.DefaultExt = "ged";

            if (!string.IsNullOrEmpty(GedComOutputTextBox.Text))
            {
                FileInfo info = new FileInfo(GedComOutputTextBox.Text);
                if ((info.Directory != null) && (info.Directory.Exists))
                {
                    dialog.InitialDirectory = info.Directory.FullName;
                }
            }

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                GedComOutputTextBox.Text = dialog.FileName;
            }
        }
    }
}